package cn.tedu.egweibo01.controller;

import cn.tedu.egweibo01.mapper.WeiboMapper;
import cn.tedu.egweibo01.pojo.dto.WeiboAddDTO;
import cn.tedu.egweibo01.pojo.entity.Weibo;
import cn.tedu.egweibo01.pojo.vo.UserVO;
import cn.tedu.egweibo01.pojo.vo.WeiboDetailVO;
import cn.tedu.egweibo01.pojo.vo.WeiboVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/weibo")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    @PostMapping("/insert")
    public int insertWeibo(@RequestBody WeiboAddDTO weiboAdd, HttpSession session) {
        UserVO userVO = (UserVO) session.getAttribute("user");
        System.out.println("weiboAdd = " + weiboAdd + ", userVO = " + userVO);
        if (userVO != null) {
            Weibo weibo = new Weibo();
            BeanUtils.copyProperties(weiboAdd, weibo);
            weibo.setUserId(userVO.getId());
            weibo.setCreated(new Date());
            return weiboMapper.insertWeibo(weibo); // 1 发布成功
        }
        return 2; // 未登录
    }

    @GetMapping("selectIndex")
    public List<WeiboVO> selectIndex() {
        System.out.println("selectIndex = " + weiboMapper.selectIndex());
        return weiboMapper.selectIndex();
    }


    @GetMapping("selectById")
    public WeiboDetailVO selectById(Integer id) {
        return weiboMapper.selectById(id);
    }

}
